/**
 * 
 */
package com.swim.sys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.swim.sys.entity.IndexBanner;
import com.swim.sys.dao.IndexBannerDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 首页bannerService
 * @author ljw
 * @version 2022-09-05
 */
@Service
@Transactional(readOnly=true)
public class IndexBannerService extends CrudService<IndexBannerDao, IndexBanner> {
	
	@Value("${server.context-path}")
	private String contextPath;
	
	/**
	 * 获取单条数据
	 * @param indexBanner
	 * @return
	 */
	@Override
	public IndexBanner get(IndexBanner indexBanner) {
		return super.get(indexBanner);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param indexBanner
	 * @return
	 */
	@Override
	public Page<IndexBanner> findPage(Page<IndexBanner> page, IndexBanner indexBanner) {
		Page<IndexBanner> resultPage = super.findPage(page, indexBanner);
		List<IndexBanner> indexBannerList = resultPage.getList();
		if(CollectionUtils.isNotEmpty(indexBannerList)) {
			List<String> indexBannerImageUrlList = new ArrayList<String>();	//首页图片url集合
			for (IndexBanner item : indexBannerList) {
				//获取首页图片
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(item.getId(), "indexBanner_image");
				if (!CollectionUtils.isEmpty(fileUploadListOne)) {
					for (FileUpload fileUpload : fileUploadListOne) {
						indexBannerImageUrlList.add(contextPath + "/userfiles/fileupload/" + fileUpload.getFileEntity().getFilePath() + fileUpload.getFileEntity().getFileId() + "." + fileUpload.getFileEntity().getFileExtension());
					}
					item.setIndexBannerImageUrlList(indexBannerImageUrlList);
				}
				break;
			}
		}
		return resultPage;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param indexBanner
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(IndexBanner indexBanner) {
		super.save(indexBanner);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(indexBanner.getId(), "indexBanner_image");
	}
	
	/**
	 * 更新状态
	 * @param indexBanner
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(IndexBanner indexBanner) {
		super.updateStatus(indexBanner);
	}
	
	/**
	 * 删除数据
	 * @param indexBanner
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(IndexBanner indexBanner) {
		super.delete(indexBanner);
	}
	
}